/**
 * PackfactoryEmployAction.java
 * Created at 2017-3-14
 * Created by shihui
 * Copyright (C) 2017 BROADTEXT SOFTWARE, All rights reserved.
 */
package com.broadtext.rms.basic.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.broadtext.common.bean.EntityPageBean;
import com.broadtext.common.exp.ServiceException;
import com.broadtext.common.utils.UUIDUtils;
import com.broadtext.rms.basic.model.TmPackfactoryEmploy;
import com.broadtext.rms.basic.service.IPackfactoryEmployService;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;

/**
 * <p>ClassName: PackfactoryEmployAction</p>
 * <p>Description: 生产厂人员管理控制层</p>
 * <p>Author: shihui</p>
 * <p>Date: 2017-3-14</p>
 */
@Controller
@RequestMapping("main/packfactoryEmploy")
public class PackfactoryEmployAction {

    @Autowired
    @Qualifier("packfactoryEmployServiceImpl")
    private IPackfactoryEmployService packfactoryEmployService;
    
    /**
     * 日志信息
     */
    public static Logger logger = LoggerFactory.getLogger(TmPackfactoryEmploy.class);
    
    /**
     * 
     * <p>Description: 获取厂区人员列表</p>
     * @param request 请求对象
     * @param response 返回对象
     */
    @RequestMapping("/getFactoryEmploys.mvc")
    @ResponseBody
    public void getFactoryEmployList(HttpServletRequest request, HttpServletResponse response) {
        try {
            PageBounds pageBounds;
            pageBounds = new PageBounds();
            Map<String, Object> map;
            String aoData;
            aoData = request.getParameter("aoData");
            JSONArray array;
            array = JSON.parseArray(aoData);
            map = new HashMap<String, Object>();
            if (array != null) {
                Iterator<?> it;
                it = array.iterator();
                while (it.hasNext()) {
                    JSONObject obj;
                    obj = (JSONObject) it.next();
                    if(obj.get("value")!=null && !"".equals(obj.get("value")+"")){
                        map.put(obj.get("name")+"", obj.get("value"));
                    }
                }
            }
            pageBounds.setLimit(Integer.parseInt(map.get("iDisplayLength").toString()));
            pageBounds.setPage(Integer.parseInt(map.get("iDisplayStart").toString()) / pageBounds.getLimit()+1);
            EntityPageBean<Object> pageBean = this.packfactoryEmployService.getFactoryEmploys(map, pageBounds);
            Map<String, Object> dataMap=new HashMap<String, Object>();
            dataMap.put("recordsTotal", pageBean.getiTotalRecords());
            dataMap.put("recordsFiltered", pageBean.getiTotalRecords());
            dataMap.put("aaData", pageBean.getResults());
            response.setCharacterEncoding("UTF-8");
            response.setContentType("text/html;charset=UTF-8");
            PrintWriter out = null;
            try {
                out = response.getWriter();
                out.write(JSON.toJSONString(dataMap));
            } catch (IOException e) {
                logger.error("查询出错",e);
                e.printStackTrace();
            } finally {
                if (out != null) {
                    out.close();
                }
            }
        } catch (ServiceException e) {
            e.printStackTrace();
        }
        
    }
    
    /**
     * 
     * <p>Description: 编辑联系人时查看联系人信息</p>
     * @param request 请求
     * @param pkId 联系人ID
     * @return 联系人信息
     */
    @RequestMapping(value = "getEmployInfo.mvc")
    @ResponseBody
    public Map<String, Object> getEmployInfo(HttpServletRequest request, String pkId) {
        Map<String, Object> map;
        map = new HashMap<String, Object>();
        try {
            map.put("data", this.packfactoryEmployService.findEntityById(pkId));
        } catch (ServiceException e) {
            e.printStackTrace();
        }
        return map;
    }
    
    /**
     * 
     * <p>Description: 保存或更新客户联系人</p>
     * @param request 请求
     * @param emp 联系人实体
     * @param response 响应
     * @return 结果
     */
    @RequestMapping(value = "saveOrUpdateEmploy.mvc")
    @ResponseBody
    public Map<String, Object> saveOrUpdateEmploy (HttpServletRequest request, TmPackfactoryEmploy emp, HttpServletResponse response) {
        Map<String, Object> map;
        map = new HashMap<String, Object>();
        
        String sysUser;
        sysUser = String.valueOf(request.getSession().getAttribute("sys.userName"));
        try {
            if (!"".equals(emp.getPkId())) {
                //update
                emp.setUpdateDate(new Date());
                emp.setUpdateUser(sysUser);
                this.packfactoryEmployService.updateRecord(emp);
            } else {
                //add
            	emp.setPkId(UUIDUtils.getUUID());
                emp.setDelFlag("0");
                emp.setUpdateDate(new Date());
                emp.setUpdateUser(sysUser);
                emp.setCreateDate(new Date());
                emp.setCreateUser(sysUser);
                this.packfactoryEmployService.insertRecord(emp);
            }
            //成功
            map.put("msg", true);
        } catch (ServiceException e) {
            e.printStackTrace();
        }
        return map;
    }
    
    /**
     * 
     * <p>Description: 删除联系人</p>
     * @param request 请求
     * @param response 响应
     */
    @RequestMapping(value = "/delete.mvc")
    @ResponseBody
    public Map<String, Object> deleteEmploys(HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map;
        map = new HashMap<String, Object>();
        String ids;
        ids = request.getParameter("pkId");
        String[] idArray;
        idArray = ids.split(",");
        String sysUser;
        sysUser = String.valueOf(request.getSession().getAttribute("sys.userName"));
        try {
            if (!this.packfactoryEmployService.checkUserBind(idArray)) {
                this.packfactoryEmployService.deleteEmployByIds(idArray, sysUser);
                map.put("msg", true);
            } else {
                map.put("msg", "bind");
            }
        } catch (ServiceException e) {
            e.printStackTrace();
        }
        return map;
    }
    
    /**
     * 
     * <p>Description: 查询相应客户下的厂区</p>
     * @param request 请求
     * @return 厂区
     */
    @RequestMapping(value = "getPackFactorys.mvc")
    @ResponseBody
    public Map<String, Object> getPackFactorys (HttpServletRequest request) {
        Map<String, Object> map;
        map = new HashMap<String, Object>();
        try {
            map.put("data", this.packfactoryEmployService.findFactory());
        } catch (ServiceException e) {
            e.printStackTrace();
        }
        return map;
    }
    
}
